package gui;

import generics.AbstractGroup;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.border.BevelBorder;

/**
 * Animates groups of movers against a white background.
 *
 * @author jianweigan, Tom Marmaduke
 */

@SuppressWarnings("serial")
public class Canvas extends JComponent {
	public static final Color BACKGROUND_COLOR = Color.WHITE;
	//list of Flocks within canvas, to support multiple flocks
	private List<AbstractGroup> myGroupList;

	/**
	 * Create a Canvas with the given size. 
	 * 
	 * @param size width and height of canvas in pixels
	 */
	public Canvas(Dimension size) {
		setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
		myGroupList = new ArrayList<AbstractGroup>();
	}

	/**
	 * Returns the list of AbstractGroups currently displayed on the Canvas  
	 */
	public List<AbstractGroup> getGroups() {
		return myGroupList;
	}

	/**
	 * Adds a group to the canvas to be animated
	 * @param group
	 */
	public void add(AbstractGroup group) {
		myGroupList.add(group);
	}

	/**
	 * Remove all shapes from the canvas.
	 */
	public void clear() {
		myGroupList.clear();
	}

	/**
	 * Paint the contents of the canvas.
	 *
	 * Never called by you directly, instead called by Java runtime
	 * when area of screen covered by this container needs to be 
	 * displayed (i.e., creation, uncovering, change in status)
	 *
	 * @param pen used to paint shape on the screen
	 */
	public void paintComponent(Graphics pen) {
		pen.setColor(BACKGROUND_COLOR);
		pen.fillRect(0, 0, getSize().width, getSize().height);
		for (AbstractGroup group : myGroupList) {
			group.paint(pen);
		}
	}

	/**
	 * Called by each step of timer, multiple times per second.
	 * 
	 * This should update the state of the animated shapes 
	 * so that they appear to move over time.
	 */
	public void update() {
		for (AbstractGroup group : myGroupList) {
			group.update(this);
		}
	}

	/**
	 * Removed group from canvas
	 * @param toRemove group to be removed
	 */
	public void removeGroup(AbstractGroup toRemove) {
		myGroupList.remove(toRemove);
	}

}